package com.xsp.eduservice.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xsp.eduservice.client.VoClient;
import com.xsp.eduservice.entity.EduVideo;
import com.xsp.eduservice.mapper.EduVideoMapper;
import com.xsp.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-02-11
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    VoClient voClient;
    //根据课程id删小节
    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.select("video_source_id");
        List<EduVideo> videos = baseMapper.selectList(wrapper);

        List<String> strings = new ArrayList<>();
        //遍历取出EduVideo的List，放入泛型为String的List中，因为批量删除的入参需要的是String类型的List
        for (EduVideo video:videos) {
            //如果取到的视频id不是空,则放入集合中
            if (!StringUtils.isEmpty(video.getVideoSourceId())){
                strings.add(video.getVideoSourceId());
            }
        }
        //如果集合中存在元素
        if (strings.size() > 0){
            voClient.removeVideoList(strings);
        }
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
    //更新视频id(video_source_id)和视频名字(video_original_name)
    @Override
    public void updateSourceIdAndName(String id) {
        EduVideo eduVideo = new EduVideo();
        eduVideo.setId(id);
        eduVideo.setVideoOriginalName("");
        eduVideo.setVideoSourceId("");
        baseMapper.updateById(eduVideo);
    }

}
